package org.samith.web.helper;


import org.samith.dao.UserDao;
import org.samith.entity.User;

public class OnlineUserSessionManager extends AbstractUserSessionManager {

	private UserDao UserDao;
	
	public OnlineUserSessionManager(UserDao UserDao){
		this.UserDao = UserDao;
	}
	
	@Override
	public AuthenticatedUserSession processRequest(LoginRequest request) {
		User user = UserDao.findByUserName(request.getUserName());
		if(user != null){
			if(user.getPassword().equals(DigestAlgorithm.getSHA1Digest(request.getPassword()))){
				AuthenticatedUserSession userSession = new AuthenticatedUserSession(false);
				userSession.setAuthenticated(true);
				userSession.setFirstName(user.getUserName());
				userSession.setUserLevel(1);
				userSession.setUserId(user.getId());
				return userSession;
			}
		}
		if(successor != null)
			return successor.processRequest(request);
		else
			return null;
	}
}
